package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

/**德阳传媒网
 * @author Administrator
 */
public class ScdytvAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://scdytv.com/newsdetails/[a-z0-9]*-[a-z0-9]*-[a-z0-9]*-[a-z0-9]*-[a-z0-9]*.cshtml"				
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();		
		if(!isDetailPage(url)){
		}				
		video.setUrl(url);		
		String title = null;
		String desc = null;
		String author = null;
		Integer clickNum = null;
		Integer commentNum = null;
		Long date = null;
		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("div.newstitle p.h1").text();
		date = DateUtils.matchDate(doc.select("p.h2").text());
		desc = HtmlCleaner.getContentHtml(url,doc.select("div.textproperty"));
		author = JSoupUtils.matchAuthor(doc, "来源：");
		if(StringUtils.isBlank(title)){
		}
		String text = doc.select("p.h2").text();
		clickNum = NumberUtils.matchNumber(StringUtils.substringBetween(text, "浏览次数", "网友评论"));
		commentNum = NumberUtils.matchNumber(StringUtils.substringAfter(text, "网友评论"));
		video.setTitle(StringUtils.trimSpace(title));
		video.setContent(desc);
		video.setDate(date);
		video.setClickNum(clickNum);
		video.setCommentNum(commentNum);
		video.setAuthor(author);
		return video;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}
	
}
